<template>
    <view class="income-info">
        <view class="c-row-box">
            <view class="c-row-head">
                <view class="c-row-title">收款信息</view>
            </view>
            <view class="c-row-item">
                <view class="c-row c-row-list">
                    <view class="c-row-col c-row-col-list-item">
                        <view class="col-title">
                            <text style="color: red;">*</text>
                            收据编号
                        </view>
                        <view class="col-text"><input @blur="checkIncome" type="text"
                                v-model="incomeParams.receivable_no" :disabled="isPreview" placeholder="请输入" /></view>
                    </view>
                </view>

                <view class="c-row c-row-list">
                    <view class="c-row-col c-row-col-list-item">
                        <view class="col-title">
                            <text style="color: red;">*</text>
                            收款方式
                        </view>
                        <view class="col-text">
                            <picker mode="selector" :range="paymentList" range-key="title" @change="incomePaymentChange"
                                :disabled="isPreview">
                                <view>{{ selectedPayment }}</view>
                            </picker>
                        </view>
                    </view>
                </view>
                <view class="c-row c-row-list">
                    <view class="c-row-col c-row-col-list-item">
                        <view class="col-title">
                            <text style="color: red;">*</text>
                            收款性质
                        </view>
                        <view class="col-text" v-if="type=='fn'">
                            <view>{{ selectedIncomeType }}</view>
                        </view>
                        <view class="col-text" v-else>
                            <picker mode="selector" :range="incomeOrderList" range-key="title"
                                @change="incomeOrderChange" :disabled="isPreview">
                                <view>{{ selectedIncomeType }}</view>
                            </picker>
                        </view>

                    </view>
                </view>

                <view class="c-row c-row-list" v-if="orderParams.order_nature != 3">
                    <view class="c-row-col c-row-col-list-item">
                        <view class="col-title">
                            <text style="color: red;">*</text>
                            收款金额
                        </view>
                        <view class="col-text">
                            <input type="digit" @blur="checkIncome" v-model="incomeParams.income_item_price"
                                @input="getTailMoney" :disabled="isPreview" placeholder="请输入" />
                        </view>
                    </view>
                </view>
                <view class="c-row c-row-list" v-else>
                    <view class="c-row-col c-row-col-list-item">
                        <view class="col-title">
                            <text style="color: red;">*</text>
                            收款金额
                        </view>
                        <view class="col-text"><input @blur="checkIncome" type="digit"
                                v-model="incomeParams.income_item_price" :disabled="isPreview" placeholder="请输入" />
                        </view>
                    </view>
                </view>

                <view class="c-row c-row-list">
                    <view class="c-row-col c-row-col-list-item">
                        <view class="col-title">
                            <text style="color: red;">*</text>
                            收款时间
                        </view>
                        <view class="col-text">
                            <picker mode="date" :start="startDate" :end="endDate" @change="incomeDateChange"
                                :disabled="isPreview">
                                <view>{{ incomeParams.income_date }}</view>
                            </picker>
                        </view>
                    </view>
                </view>
            </view>
        </view>
    </view>
</template>

<script>
import dictionary from '@/utils/dictionary.js';
import request from '@/utils/request.js';
export default {
    props: {
        isPreview: {
            type: Boolean,
            default() {
                return false;
            }
        },
        incomeParams: {
            type: Object,
            default() {
                return {};
            }
        },
        orderParams: {
            type: Object,
            default() {
                return {};
            }
        },
        incomeNature: {
            type: [Number, String],
            default() {
                return null;
            }
        },
        incomeType: {
            type: String,
            default() {
                return '';
            }
        },
        startDate: {
            type: String,
            default() {
                return '';
            }
        },
        endDate: {
            type: String,
            default() {
                return '';
            }
        }
    },
    data() {
        return {
            incomeOrder: [{
                id: 1,
                title: '定金'
            },
            {
                id: 7,
                title: '全款'
            }
            ],
            incomeOrderList: [],
            type: ""
        };
    },
    computed: {
        paymentList: function () {
            return dictionary.getPicker('paymentList');
        },
        selectedPayment: function () {
            return dictionary.getPickerTitle('paymentList', this.incomeParams.income_payment, '请选择');
        },
        selectedIncomeType: function () {
            return dictionary.getPickerTitle('exchangeTypeList', this.incomeParams.income_type, '请选择');
        }
    },
    beforeMount() {
        this.dictionary = dictionary;
    },
    mounted() {
        this.incomeOrderList = uni.getStorageSync('allPicker').exchangeTypeList;
    },
    methods: {
        //验证收款性质
        getIncomeNature(type, value) {
            let _this = this;
            //创建订单
            if (type == 'order') {
                _this.incomeOrderList = _this.incomeOrder
            }

            // 收款
            if (type == 'income') {
                _this.incomeOrderList = _this.incomeOrderList
            }

            //自定义
            if (type == 'fn') {
                _this.type = type;
                _this.incomeParams.income_type = value[0].id
                _this.incomeOrderList = value
            }
        },
        // 检查收款编号
        verificationCode(collection_code) {
            if (collection_code != '') {
                request.request({
                    url: '/api/order.collection_code/unique',
                    showLoading: true,
                    token: true,
                    data: {
                        collection_code: collection_code
                    },
                    success(res) {
                        if (res.code == 200) {
                            uni.showToast({
                                icon: 'none',
                                title: res.msg,
                                duration: 3000
                            });
                        } else {
                            uni.showToast({
                                icon: 'none',
                                title: res.msg,
                                duration: 3000
                            });
                        }
                    }
                });
            }
        },
        checkIncome() {
            let _this = this;
            let incomePrice = _this.incomeParams.income_item_price;
            if (incomePrice > 0) {
                _this.verificationCode(_this.incomeParams.receivable_no)
            }
        },
        // 收款日期
        incomeDateChange(e) {
            //收入时间
            this.incomeParams.income_date = e.target.value;
            this.orderParams.earnest_money_date = e.target.value;
        },

        // 收款方式
        incomePaymentChange(e) {
            let index = e.target.value;
            this.incomeParams.income_payment = this.paymentList[index].id;
        },

        // 当是订单时
        incomeOrderChange(e) {
            let index = e.target.value;

            // if (this.incomeType == '13') {
            //     this.incomeParams.income_type = this.incomeOrderList[index].id;
            // } else {
            //     this.incomeParams.income_type = this.incomeOrder[index].id;
            // }
            this.incomeParams.income_type = this.incomeOrderList[index].id;

            // 这种方式不是两个变量，本质上是一个，因为这两个变量操作的是同一个内存地址。
            if (e.target.value == 1) {
                let orderParamsData = this.orderParams;
                orderParamsData.tail_money_date = '';
                orderParamsData.tail_money = ''
            }
            // 当为全款时，this.incomeType == '7', 重置定金、中款、尾款
            if (this.incomeParams.income_type == '7') {
                // 重置订单的收款信息、定金、中款、尾款
                this.$emit('resetIncome');
            }
        },

        // 自动计算尾款
        getTailMoney(e) {
			this.orderParams.middle_money = '';
			this.orderParams.middle_money_date = ''
            if (this.incomeParams.income_type != '7') {
                this.$emit('getTailMoney');
            }
			if (this.orderParams.news_type == '7' && this.orderParams.order_nature == '1') {
				this.orderParams.middle_money = '0';
				this.orderParams.middle_money_date = '2023-08-31'
			}
        }
    }
};
</script>

<style lang="less">
@import url("../../components/common.less");
@import url("./common.less");
</style>
